<?php

namespace Beerus\Modules\VirtualPhone\Actions;

use Encore\Admin\Actions\RowAction;
use hhz\constant\basic\UserExtendPhoneScene;
use Illuminate\Http\Request;
use Doraemon\model\VirtualPhone\DesignerVirtualPhoneRecord;
use Doraemon\tools\AlibabaCloud\VirtualPhone;
use Doraemon\service\BusinessUser\EncryptPhone;

class UpdateVirtualInfo extends RowAction
{
    public $name = '修改绑定信息';

    public function handle($id,Request $request)
    {
        $bindPhoneA= $request->get('bindPhoneA');
        $virtualInfo = DesignerVirtualPhoneRecord::getByCondition(['id' => $id]);
        $subsId = $virtualInfo['subs_id'];
        $virtualPhone = $virtualInfo['virtual_phone'];
        $virtualExpireTime = $virtualInfo['virtual_expire_time'];
        $nowTime = date("Y-m-d H:i:s");
        if ($virtualExpireTime >= $nowTime){
            //未过期的号码才可以进行修改
            VirtualPhone::UpdateVirtualInfo($subsId,$virtualPhone,$bindPhoneA);
        }
        //修改表中设计师的信息
        $encrypt_designer_phone = EncryptPhone::createEncryptPhone($bindPhoneA);
        $encrypt_designer_id= EncryptPhone::getEncryptPhoneId($bindPhoneA,UserExtendPhoneScene::DESIGNER_VIRTUAL_PHONE);
        $hidden_designer_phone = EncryptPhone::getHiddenPhone($bindPhoneA);
        DesignerVirtualPhoneRecord::update([
            'designer_phone' => $bindPhoneA,
            'encrypt_designer_phone' => $encrypt_designer_phone,
            'encrypt_designer_id' => $encrypt_designer_id,
            'hidden_designer_phone' => $hidden_designer_phone,
        ], ['id' => $id]);

        return $this->response()->success('修改设计师手机号成功!')->refresh();
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }
        return $key;
    }

    public function form($request)
    {
        $this->hidden('id')->value($this->row->id);
        $this->text('bindPhoneA', '修改绑定的设计师号码')->help('修改后该号码后,设计师只能用该号码拨打!!');
        //$this->text('bindPhoneB', '修改绑定的用户号码')

    }
}
